package Week.w145;

import org.junit.Test;

/**
 * @program Leetcode
 * @description:
 * @author: lishangsheng
 * @create: 2019/07/15 18:08
 */
public class problem01 {
    @Test
    public void test() {
        int[] arr1=new int[]{2,3,1,3,2,4,6,7,9,2,19};
        int[] arr2=new int[]{2,1,4,3,9,6};
        arr1= relativeSortArray(arr1,arr2);
        System.out.println(arr1);
    }

    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        if (arr2 == null || arr2.length == 0) {
            return arr1;
        }
        int index = 0;
        for (int i = 0; i < arr2.length; i++) {
            int search = arr2[i];
            for (int j = index; j < arr1.length; j++) {
                if (arr1[j] == search) {
                    swap(arr1, index, j);
                    index++;
                }
            }
        }
        //剩下的从小到大排序
        for (int i = index; i < arr1.length-1; i++)
        {
            for(int j=i+1;j<arr1.length;j++){
                if(arr1[i]>arr1[j]){
                    swap(arr1,i,j);
                }
            }
        }
        return arr1;

    }

    private void swap(int[] array,int index,int searchIndex){
        int temp=array[searchIndex];
        array[searchIndex]=array[index];
        array[index]=temp;
    }

}
